<?php
/**
* This is controller use for public area
* @author: phutp@younetco.com
* @version: 0.1
*/


class Contest_IndexController extends YouNet_Controller_Action{
	
	public function preDispatch() {
		parent::preDispatch();
		$this->view->use_admin_menu = 1;		
	}
	/**
	* This action is the first view when accessing the app
	*/
	public function indexAction()
	{
		$this->_forward('list','manage','contest');
	}
	
	public function shareAction()
	{
		if(!Core_Util::isFacebookRequest())
		{
			$this->_forward('list','manage','contest');
		}
	}
    public function notifyAction()
    {
        $this->disableAutoRender();
        
        $contestsTable = YouNet_Util::getDbTable("contests");
        $contest = $contestsTable->fetchRow(array(
            'id=?' => $this->_params['id_contest']
        ));
        
        if(!empty($contest->notify_email))
        {
            $id_page = $this->_social->getIdPage();
            $pagesTable = Core_Model_DbTable_Pages::getInstance();
            $name_page = $pagesTable->getNamePage( $id_page);

            $from       = "support.contest@younetmedia.com";
            $subject    = "[".$name_page. "][" . $contest->title . ']: New Comment on Your Page';
            $template   = "templateCommentNotify.phtml";
            $notify_email = preg_replace('/\s+/', "", $contest->notify_email);
            if(!empty($notify_email))
            {    
                $to         = explode( ",", $notify_email);
                $data = array(
                    'subject'   => $subject,
                    'url'       => $this->_params['url'],
                    'title'     => $contest->title,
                    "user_name" => $this->_params['user_name'],
                    "is_contest"=> isset($this->_params['is_contest']) ? $this->_params['is_contest'] : ""
                );
                if(isset($this->_params['entry_name'])){
                    $data["entry_name"] = $this->_params['entry_name'] ;
                }
                
                foreach($to as $mod_email)
                {
                    $data['mod_email'] = $mod_email;
                    $this->_sendEmail($from, $mod_email, $subject, $template, $data);
                }
            }    
        }
    }
    
    protected function _sendEmail($from, $to, $subject, $template, $data)
    {
        try{
            $mail			= new Zend_Mail('UTF-8');
            $admin_mail		= $from;
            $mail->setFrom($from);
            $mail->setBodyHtml($this->view->partial($template, $data));
            $mail->addTo($to);
            $mail->setSubject($subject);
            $mail->send();
        }  catch (Exception $e){};
    }
}